package cn.hs.dao.impl;

import cn.hs.dao.GoodsDao;
import cn.hs.entity.Goods;
import cn.hs.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {

    @Override
    public List<Goods> selectGoods(Map<String, String> map) {
        List<Goods> list = new ArrayList<>();
        //动态sql
        StringBuffer sb = new StringBuffer("select * from goods where 1=1 ");
        //动态参数
        List param = new ArrayList();
        String goodsStatus = map.get("goodsStatus");
        if ("1".equals(goodsStatus)||"0".equals(goodsStatus)){
            sb.append(" and goodsStatus = ? ");
            param.add(goodsStatus);
        }
        //
        String goodsName = map.get("goodsName");
        if (goodsName!=null && !"".equals(goodsName)){
            sb.append(" and goodsName like ? ");
            param.add("%"+goodsName+"%");
        }
        //
        String shopName = map.get("shopName");
        if (shopName!=null && !"".equals(shopName)){
            sb.append(" and goodsShopName like ? ");
            param.add("%"+shopName+"%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sb.append(" limit ?,? ");
        param.add((pageNum-1)*pageSize);
        param.add(pageSize);
        String sql = sb.toString();
        Object[] objects = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, objects);
        try {
            while (resultSet.next()){
                Goods goods = new Goods(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5),
                        resultSet.getInt(6),
                        resultSet.getInt(7),
                        resultSet.getDate(8),
                        resultSet.getString(9),
                        resultSet.getInt(10),
                        resultSet.getString(11),
                        resultSet.getInt(12)
                );
                list.add(goods);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectGoodsCount(Map<String, String> map) {
        int count = 0;
        //动态sql
        StringBuffer sb = new StringBuffer("select count(*) from goods where 1=1 ");
        //动态参数
        List param = new ArrayList();
        String goodsStatus = map.get("goodsStatus");
        if ("1".equals(goodsStatus)||"0".equals(goodsStatus)){
            sb.append(" and goodsStatus = ? ");
            param.add(goodsStatus);
        }
        //
        String goodsName = map.get("goodsName");
        if (goodsName!=null && !"".equals(goodsName)){
            sb.append(" and goodsName like ? ");
            param.add("%"+goodsName+"%");
        }
        //
        String shopName = map.get("shopName");
        if (shopName!=null && !"".equals(shopName)){
            sb.append(" and goodsShopName = ? ");
            param.add("%"+shopName+"%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        String sql = sb.toString();
        Object[] objects = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, objects);
        try {
            if (resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }

    @Override
    public int update(String goodsId, String goodsStatus) {
        String sql ="update goods set goodsStatus = ? where goodsId=?";
        int row=0;
        Object object[]={goodsStatus,goodsId};
        row = JDBCDruidUtil.update(sql, object);
        return row;
    }
}
